home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 August / macformat-027.iso / mac / Shareware City / Developers / Oberon⁄F / Samples / Mod / Ex8 (.txt) < prev    next >
Encoding:
Oberon Document  |  1994-06-07  |  4.6 KB  |  108 lines  |  [oODC/obnF]

  1. Documents.StdDocumentDesc
  2. Documents.DocumentDesc
  3. Containers.ViewDesc
  4. Views.ViewDesc
  5. Stores.StoreDesc
  6. Documents.ModelDesc
  7. Containers.ModelDesc
  8. Models.ModelDesc
  9. Stores.ElemDesc
  10. TextViews.StdViewDesc
  11. TextViews.ViewDesc
  12. TextModels.StdModelDesc
  13. TextModels.ModelDesc
  14. TextModels.AttributesDesc
  15. Geneva
  16. Geneva
  17. Geneva
  18. Geneva
  19. DevCommanders.StdViewDesc
  20. DevCommanders.ViewDesc
  21. Example 8
  22. This example provides two commands to the user. The first, EnterData, takes a text selection as input, and reads it line for line. Each line should consist of an integer number, followed by a string, trailed by a real number. Each such tuple is entered into a globally anchored linear list, sorted by the integer value. The second command, ListData, generates a text which displays the data currently in the list.
  23. The following text is not an embedded text view, since embedded views which are larger than one page tend to be cumbersome to use. To compile a text like the one below, select its beginning (e.g. double-click on the keyword MODULE), and then execute Compile
  24. Selection from the Dev menu.
  25. MODULE SamplesEx8;
  26.     IMPORT Views, TextModels, TextMappers, TextViews, TextControllers;
  27.     CONST
  28.         int = TextMappers.int; string = TextMappers.string;
  29.         real = TextMappers.real; invalid = TextMappers.invalid;
  30.     TYPE
  31.         Node = POINTER TO NodeDesc;
  32.         NodeDesc = RECORD
  33.             next: Node;
  34.             id: LONGINT;
  35.             name: TextMappers.String;
  36.             value: REAL
  37.         END;
  38.     VAR list: Node;
  39.     PROCEDURE Enter (id: LONGINT; name: TextMappers.String; value: REAL);
  40.         VAR n, h, p: Node;
  41.     BEGIN
  42.         NEW(n); n.id := id; n.name := name; n.value := value;
  43.         h := list; p := NIL; WHILE (h # NIL) & (h.id <= id) DO p := h; h := h.next END;
  44.         IF p # NIL THEN    (* insert between p and h *)
  45.             p.next := n
  46.         ELSE    (* insert at beginning *)
  47.             list := n
  48.         END;
  49.         n.next := h
  50.     END Enter;
  51.     PROCEDURE EnterData*;
  52.         VAR c: TextControllers.Controller; beg, end: LONGINT;
  53.             s: TextMappers.Scanner; id: LONGINT;
  54.             name: TextMappers.String; value: REAL;
  55.     BEGIN
  56.         c := TextControllers.Focus();
  57.         IF (c # NIL) & c.HasSelection() THEN
  58.             c.GetSelection(beg, end);
  59.             s.ConnectTo(c.text); s.SetPos(beg);
  60.             s.Scan;
  61.             WHILE (s.type = TextMappers.int) & (s.Pos() <= end) DO
  62.                 IF s.type = int THEN id := s.int; s.Scan ELSE s.type := TextMappers.invalid END;
  63.                 IF s.type = string THEN name := s.string; s.Scan ELSE s.type := invalid END;
  64.                 IF s.type = real THEN value := SHORT(s.real); s.Scan ELSE s.type := invalid END;
  65.                 Enter(id, name, value)
  66.             END;
  67.             c.SelectAll(FALSE)
  68.         END
  69.     END EnterData;
  70.     PROCEDURE ListData*;
  71.         VAR t: TextModels.Model; n: Node; f: TextMappers.Formatter;
  72.     BEGIN
  73.         t := TextModels.dir.New();
  74.         f.ConnectTo(t); f.SetPos(0);
  75.         n := list;
  76.         WHILE n # NIL DO
  77.             f.WriteInt(n.id); f.WriteTab; f.WriteString(n.name); f.WriteTab; f.WriteReal(n.value);
  78.             f.WriteLn;
  79.             n := n.next
  80.         END;
  81.         Views.Open(TextViews.dir.New(t), NIL, "")
  82.     END ListData;
  83.     PROCEDURE Reset*;
  84.     BEGIN
  85.         list := NIL    (* release data structure so that garbage collector can reclaim memory *)
  86.     END Reset;
  87. END SamplesEx8.
  88.  SamplesEx8.EnterData        
  89.  SamplesEx8.ListData        
  90.  SamplesEx8.Reset
  91. To try out the example, select the following lines, and then click the left commander above:
  92. 1    Cray    14.8
  93. 3    NEC    16.6
  94. 2    IBM    8.3
  95. Now click the middle commander, as a result a window opens with the sorted input. If you repeat both steps, you'll note that the input has been added to the list a second time, and that consequently every item appears twice in the output.
  96. This example has shown how a text can be scanned symbol by symbol, instead of character by character.
  97. TextControllers.StdCtrlDesc
  98. TextControllers.ControllerDesc
  99. Containers.ControllerDesc
  100. Controllers.ControllerDesc
  101. TextRulers.StdRulerDesc
  102. TextRulers.RulerDesc
  103. TextRulers.StdStyleDesc
  104. TextRulers.StyleDesc
  105. TextRulers.AttributesDesc
  106. Geneva
  107. Documents.ControllerDesc
  108.